package com.app.controller;

import cn.hutool.core.util.StrUtil;
import com.app.common.Result;
import com.app.mapper.EntrepreneursMapper;
import com.app.model.*;
import com.app.service.*;
import com.app.utils.TokenUtils;
import com.app.vo.StatisticsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/statistics")
public class StatisticsController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private IntegralOrderService integralOrderService;
    @Autowired
    private BusinessSettledService businessSettledService;
    @Autowired
    private EntrepreneursService entrepreneursService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private UserService userService;
    @Autowired
    private AdminUserService adminUserService;

    @Autowired
    private VoucherRecordService voucherRecordService;

    /**
     * 查询状态点
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return {@link Result}
     */
    @GetMapping("/queryStatusPoints")
    public Result queryStatusPoints(@RequestParam(value = "startTime",required = false) String startTime,
                                    @RequestParam(value = "endTime",required = false) String endTime) {

        return Result.success(this.integralOrderService.queryStatusPoints(startTime,endTime));
    }


    /**
     * 总使用优惠券
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return {@link Result}
     */
    @GetMapping("/totalUsageCoupons")
    public Result totalUsageCoupons(@RequestParam(value = "startTime",required = false) String startTime,
                                    @RequestParam(value = "endTime",required = false) String endTime) {

        return Result.success(this.voucherRecordService.totalUsageCoupons(startTime,endTime));
    }


    @GetMapping("/getCountAll")
    public Result getCountAll(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        StatisticsVo vo = new StatisticsVo();
        QueryWrapper<OrderModel> orderModelQueryWrapper = new QueryWrapper<OrderModel>();
        QueryWrapper<IntegralOrderModel> inviteRecordModelQueryWrapper=new QueryWrapper<>();
        QueryWrapper<BusinessModel> businessModelQueryWrapper = new QueryWrapper<BusinessModel>().eq("status", 1);
        QueryWrapper<BusinessSettledModel> businessSettledModelQueryWrapper = new QueryWrapper<>();
        QueryWrapper<EntrepreneursModel> entrepreneursModelQueryWrapper = new QueryWrapper<>();
        QueryWrapper<UserModel> userModelQueryWrapper = new QueryWrapper<>();
        // 根据权限添加查询条件
        if (user.getRoleId()==2) { // 如果是商家
            // 查询当前商家的信息
            businessModelQueryWrapper.eq("id",user.getBusinessId());
        } else if(user.getRoleId()==3){ //市代理
            if (user.getAreaId()==null){
                return Result.success(vo);
            }
            businessModelQueryWrapper.eq("city_id",user.getAreaId());
            entrepreneursModelQueryWrapper.eq("city_id",user.getAreaId());
            businessSettledModelQueryWrapper.eq("city_id",user.getAreaId());
            userModelQueryWrapper.eq("city_id",user.getAreaId());
            orderModelQueryWrapper.eq("city_id",user.getAreaId());
            inviteRecordModelQueryWrapper.eq("city_id",user.getAreaId());
        }else if(user.getRoleId()==4){ // 区代理
            // do nothing，普通用户无需额外权限限制
            businessModelQueryWrapper.eq("area_id",user.getAreaId());
            entrepreneursModelQueryWrapper.eq("area_id",user.getAreaId());
            businessSettledModelQueryWrapper.eq("area_id",user.getAreaId());
            userModelQueryWrapper.eq("area_id",user.getAreaId());
            orderModelQueryWrapper.eq("area_id",user.getAreaId());
            inviteRecordModelQueryWrapper.eq("area_id",user.getAreaId());
        }
        vo.setBusinessCount(businessService.count(businessModelQueryWrapper));
        vo.setEntrepreneursCount(entrepreneursService.count(entrepreneursModelQueryWrapper));
        vo.setSettleInCount(businessSettledService.count(businessSettledModelQueryWrapper));
        vo.setUserCount(userService.count(userModelQueryWrapper));
        vo.setActivityOrderCount(orderService.count(orderModelQueryWrapper));
        vo.setIntegralOrderCount(integralOrderService.count(inviteRecordModelQueryWrapper));
        return Result.success(vo);
    }
}
